<script setup lang="ts">
	import { ref } from "vue";
	import {
		validatePhoneNumber
	} from '@/utlis/common.js'
	import {
		onShow,
		onHide,
		onLoad
	} from '@dcloudio/uni-app';
	import {
		getCurrentInstance
	} from 'vue'
	import {
		useRouter
	} from 'uni-mini-router'
	import {
		userStore
	} from '@/pinia';
	const userStoreInfo = userStore();
	const {
		proxy,
	} = getCurrentInstance()
	let router = useRouter()
	onLoad(() => {
		if (uni.getStorageSync('roleFlag') == 1) {
			//商家价格
			proxy.$req('GET', proxy.$api.getBusPrice, {}).then((res) => {
				console.log(res);
				form1.value.money = `￥${res.result.value}`
			})
		} else if (uni.getStorageSync('roleFlag') == 2) {
			//供应商价格
			proxy.$req('GET', proxy.$api.getGysPrice, {}).then((res) => {
				console.log(res);
				form1.value.money = `￥${res.result.value}`
			})
		}
	})
	let qForm = ref()
	let form1 = ref({
		name: '',
		code: '',
		human: '',
		phone: '',
		address: '',
		money: ''
	})
	let rules = ref({
		name: {
			type: 'string',
			required: true,
			message: '输入不合法',
			// min: 5,
			// trigger: ['change']
		},
		code: {
			type: 'string',
			required: true,
			message: '输入不合法',
			// trigger: ['change']
		},
		human: {
			type: 'string',
			required: true,
			message: '输入不合法',
			// trigger: ['change']
		},
		phone: [{
			required: true,
			message: '手机号码不正确',
		},
		{
			validator: (rule, value, callback) => {
				console.log(value);
				return validatePhoneNumber(value);
			},
			message: '手机号码不正确',
			// trigger: ['change'],
		}],
		address: {
			type: 'string',
			required: true,
			message: '输入不合法',
			// trigger: ['change']
		},
		money: {
			type: 'string',
			required: false,
			message: '输入不合法',
			// trigger: ['change']
		},
	})
	let ocrOk = ref(false)
	let fileList = ref({
		fileList1: [],
		fileList2: [],
		fileList3: []
	})
	const afterRead = (e, type) => {
		const item = e.file[0];
		fileList.value[`fileList${type}`].push({
			...item,
			//提示上传中
			status: 'uploading',
			message: '上传中'
		});
		uni.uploadFile({
			url: proxy.$uploadUrl + proxy.$api.uploadFile,
			filePath: e.file[0].url,
			name: 'file',
			formData: {
				file: e.file[0].url,
			},
			success: (uploadFileRes) => {
				// console.log(uploadFileRes);
				let result = JSON.parse(uploadFileRes.data).result
				fileList.value[`fileList${type}`].splice(
					0,
					1,
					Object.assign(item, {
						status: 'success',
						message: '上传成功',
						url: result,
					})
				);
				console.log(fileList.value[`fileList${type}`]);
			},
			complete() { }
		});
	}
	const deletePic = (e, type) => {
		fileList.value[`fileList${type}`] = []
	}
	const submit = () => {
		if (userStoreInfo.userInfo.supplier != '' && userStoreInfo.userInfo.supplier.payNum != null && userStoreInfo.userInfo.supplier.isReal != -1) {
			return
		}
		if (userStoreInfo.userInfo.shoppingStore != '' && userStoreInfo.userInfo.shoppingStore.payNum != null && userStoreInfo.userInfo.shoppingStore.isReal != -1) {
			return
		}

		if (fileList.value.fileList1.length < 1) {
			uni.$u.toast('请上传营业执照')
			return
		} else if (fileList.value.fileList2.length < 1) {
			uni.$u.toast('请上传人像面')
			return
		} else if (fileList.value.fileList3.length < 1) {
			uni.$u.toast('请上传国徽面')
			return
		}
		//提交认证
		let url, data;
		if (uni.getStorageSync('roleFlag') == 1) {
			//商家提交	
			url = proxy.$api.busAuth
			data = {
				id: userStoreInfo.userInfo.shoppingStore.id,
				storeEnterpriseName: form1.value.name,
				unified_credit_code: form1.value.code,
				storeUser: form1.value.human,
				storePhone: form1.value.phone,
				storePic: fileList.value.fileList1[0].url,
				storeIdcardA: fileList.value.fileList2[0].url,
				storeIdcardB: fileList.value.fileList3[0].url,
				storeAddress: form1.value.address
			}
		} else if (uni.getStorageSync('roleFlag') == 2) {
			//供应商提交
			url = proxy.$api.gysAuth
			data = {
				id: userStoreInfo.userInfo.supplier.id,
				enterpriseName: form1.value.name,
				unifiedCreditCode: form1.value.code,
				contactsUser: form1.value.human,
				contactsPhone: form1.value.phone,
				businessLicenseImage: fileList.value.fileList1[0].url,
				frontOfIdCard: fileList.value.fileList2[0].url,
				reverseOfIdCard: fileList.value.fileList3[0].url,
				storeAddress: form1.value.address
			}
		}
		qForm.value.validate().then(res => {
			uni.$u.toast('校验通过')
			proxy.$req('POST', url, data).then((res) => {
				if (res.success) {
					let url = JSON.parse(res.result).resp_data.counter_url
					console.log(url);
					tradeNo.value = JSON.parse(res.result).resp_data.out_order_no
					payId.value = JSON.parse(res.result).resp_data.pay_order_no
					// #ifdef APP-PLUS
					plus.runtime.openURL(url)
					console.log('app');
					// #endif
					// #ifdef H5
					window.open(url)
					console.log('h5');
					// #endif
					//支付成功或者失败的回调缺少
				}
			})
		}).catch(errors => {
			uni.$u.toast('校验失败')
		})
	}
	let tradeNo = ref('')
	let payId = ref('')
	onShow(() => {
		userStoreInfo.updateUserInfo(userStoreInfo.userInfo.id)
		if (tradeNo.value != '' && payId.value != '') {
			isPaySuccess()
		}
	})
	const isPaySuccess = () => {
		proxy.$req('POST', proxy.$api.ShoppingStoreOrder, {
			tradeNo: tradeNo.value,
			payId: payId.value
		}).then((res) => {
			if (res.code != 200 || res.result == null) {
				console.log("出错了")
				uni.$u.toast(res.message)
				return
			}
			let a = JSON.parse(JSON.stringify(res)).result
			let data = JSON.parse(a).resp_data
			if (data.order_status == 0) {
				uni.$u.toast('待支付')
			} else if (data.order_status == 1) {
				uni.$u.toast('支付中')
			} else if (data.order_status == 2) {
				uni.$u.toast('支付成功,请等待审核')
				router.pushTab({
					name: 'index',
				})
			} else if (data.order_status == 3) {
				uni.$u.toast('支付失败')
			} else if (data.order_status == 4) {
				uni.$u.toast('订单已过期')
			} else if (data.order_status == 5) {
				uni.$u.toast('订单已取消')
			} else if (data.order_status == 6) {
				uni.$u.toast('部分退款或者全部退款')
			}
		})
	}
	let addressFlag = ref(0)
	const getMapChoseAddress = () => {
		if (addressFlag.value == 1) return
		addressFlag.value = 1
		console.log('diao--------------');
		uni.chooseLocation({
			success: function (res) {
				console.log(res);
				console.log('位置名称：' + res.name);
				console.log('详细地址：' + res.address);
				console.log('纬度：' + res.latitude);
				console.log('经度：' + res.longitude);
				if (res.address) {
					form1.value.address = res.address
				}
				addressFlag.value = 0
			},
			fail() {
				addressFlag.value = 0
			}
		});
	}
	const routerLink = () => {
		router.pushTab({
			name: 'index',
			params: {}
		})
	}
</script>
<template>
	<u-navbar :placeholder="true" title="认证信息" :autoBack="true">
		<template #right>
			<view class="" @click="routerLink">
				跳过
			</view>
		</template>
	</u-navbar>
	<view class="formBox">
		<u--form labelPosition="left" :model="form1" ref="qForm" :rules="rules" labelWidth="170rpx">
			<text style="font-size: 28rpx;"><text style="color: red;">*</text>营业执照</text>
			<u-form-item label="" prop="" ref="">
				<view style="display: flex;flex-direction: column;">
					<u-upload :fileList="fileList.fileList1" @afterRead="afterRead($event,1)"
						@delete="deletePic($event,1)" name="6" multiple :maxCount="1" width="100" height="100">
					</u-upload>
				</view>
			</u-form-item>
			<text style="font-size: 28rpx;"><text style="color: red;">*</text>法人身份证</text>
			<u-form-item label="" prop="" ref="">
				<view style="display: flex;">
					<view class="c-flex-center-center">
						<u-upload :fileList="fileList.fileList2" @afterRead="afterRead($event,2)"
							@delete="deletePic($event,2)" name="6" multiple :maxCount="1" width="100" height="100">
						</u-upload>
						<text style="margin-top: 10rpx;">人像面</text>
					</view>
					<view class="c-flex-center-center">
						<u-upload :fileList="fileList.fileList3" @afterRead="afterRead($event,3)"
							@delete="deletePic($event,3)" name="6" multiple :maxCount="1" width="100" height="100">
						</u-upload>
						<text style="margin-top: 10rpx;">国徽面</text>
					</view>
				</view>
			</u-form-item>
			<u-form-item label="企业名称" prop="name" borderBottom ref="" required>
				<u--input class="inputClass" v-model="form1.name" :disabled="ocrOk" disabledColor="#ffffff"
					border="none" placeholder="企业名称"></u--input>
			</u-form-item>
			<u-form-item label="社会统一信用代码" prop="code" borderBottom ref="" required>
				<u--input class="inputClass" v-model="form1.code" :disabled="ocrOk" disabledColor="#ffffff"
					placeholder="社会统一信用代码" border="none"></u--input>
			</u-form-item>
			<u-form-item label="联系人" prop="human" borderBottom ref="" required>
				<u--input class="inputClass" v-model="form1.human" :disabled="ocrOk" disabledColor="#ffffff"
					placeholder="联系人" border="none"></u--input>
			</u-form-item>
			<u-form-item label="联系电话" prop="phone" borderBottom ref="" required>
				<u--input class="inputClass" v-model="form1.phone" disabledColor="#ffffff" placeholder="联系电话"
					border="none"></u--input>
			</u-form-item>
			<u-form-item label="门店地址" prop="address" borderBottom ref="" required @click.stop="getMapChoseAddress">
				<u--input class="inputClass" v-model="form1.address" disabled disabledColor="#ffffff" placeholder="门店地址"
					border="none" :customStyle="{pointerEvents:'none'}"></u--input>
				<u-icon name="arrow-right"></u-icon>
			</u-form-item>
			<u-form-item label="认证费用" prop="money" borderBottom ref="" required>
				<u--input class="inputClass" v-model="form1.money" disabled disabledColor="#ffffff" placeholder="认证费用"
					border="none"></u--input>
			</u-form-item>
		</u--form>
	</view>
	<view style="width: 94%;margin: 0 auto;">
		{{ userStoreInfo.userInfo.supplier==''?'': userStoreInfo.userInfo.supplier.isReal==-1?'拒绝原因：'+ userStoreInfo.userInfo.supplier.message:''}}
		{{ userStoreInfo.userInfo.shoppingStore==''?'': userStoreInfo.userInfo.shoppingStore.isReal==-1?'拒绝原因：'+ userStoreInfo.userInfo.shoppingStore.message:''}}
	</view>
	<view class="submit" @click="submit">
		{{ userStoreInfo.userInfo.supplier==''?'':userStoreInfo.userInfo.supplier.isReal==-1?'审核失败请重新提交': userStoreInfo.userInfo.supplier.payNum!=null?'审核中请跳过':'支付并提交审核'}}
		{{ userStoreInfo.userInfo.shoppingStore==''?'':userStoreInfo.userInfo.shoppingStore.isReal==-1?'审核失败请重新提交': userStoreInfo.userInfo.shoppingStore.payNum!=null?'审核中请跳过':'支付并提交审核'}}
	</view>
</template>
<style lang="scss">
	.formBox {
		width: calc(100% - 60rpx);
		padding: 0 30rpx;

		:deep() {
			.inputClass {
				.u-input__content__field-wrapper__field {
					text-align: right !important;
				}
			}
		}
	}

	.submit {
		width: 94%;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		font-size: 26rpx;
		margin: 80rpx auto 0;
		background-color: #CC645E;
		color: #fff;
		border-radius: 10rpx;
	}
</style>